
package com.starbaba.credit.view;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.base.service.CreditEntranceService;
import com.starbaba.base.service.impl.CreditEntranceServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.credit.service.CreditOrderService;
import com.starbaba.credit.service.impl.CreditOrderServiceImpl;
import com.starbaba.util.QueryListUtil;
import com.tuanzi.server.loan_base.model.db.credit.PayOrder;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.apache.struts2.ServletActionContext;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 征信订单后台action
 * @author chenweijun
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class CreditOrderAction extends BaseAction implements ModelDriven<PayOrder> {

    private static final long serialVersionUID = 1L;
    private PayOrder model;
	private CreditOrderService service;
	private CreditEntranceService creditEntranceService;
	private InputStream inputStream;   //导出文件输入流
	private String fileName;   //导出文件名

	public CreditOrderAction(){
		this.service = new CreditOrderServiceImpl();
		this.creditEntranceService = new CreditEntranceServiceImpl();
	}

	@Override
	public PayOrder getModel() {
		if (model==null) {
			model=new PayOrder();
		}
		return model;
	}
	
	/**
	 * 查询征信订单列表
	 */
	public void getCreditOrderList(){
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		List<PayOrder> list=service.getCreditOrderList(paramsMap, pageRequest.getIDisplayStart(),pageRequest.getIDisplayLength());
		int count=service.getCreditOrderCount(paramsMap);
		pageResponse.setITotalDisplayRecords(count);
		pageResponse.setITotalRecords(count);
		try {
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(IOException e) {
			LogService.error(e);
		}
	}
	
	/**
	 * 更新订单状态
	 */
	public String updateCreditOrderStatus(){
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String operator = getUserVO().getUsername();
		paramsMap.put("operator", operator);
		int result = service.updateCreditOrderStatus(paramsMap);
		if(result > 0){
			return SUCCESS;
		}
		return ERROR;
	}
	
	/**
	 * 导出数据到excel表格
	 * @return
	 */
	public String exportCreditOrder(){
		Map<String, Object> params = QueryListUtil.getRequestParamsMap(ServletActionContext.getRequest());
        QueryListUtil.dropEmptyParams(params);
        String result = service.exportCreditOrderList(params);
        try {
			  inputStream = new ByteArrayInputStream(result.getBytes("GBK"));
			  fileName = "credit_order_export_" + System.currentTimeMillis() + ".csv";
		} catch (UnsupportedEncodingException e) {
			LogService.error(e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 查询征信入口列表
	 */
	public void getCreditEntranceList(){
		
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		List list = creditEntranceService.getCreditEntranceList();
		pageResponse.setITotalDisplayRecords(list.size());
		pageResponse.setITotalRecords(list.size());
		try {
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(IOException e) {
			LogService.error(e);
		}
		
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
}
